package com.zyb.warehouse.controller;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.dto.AssignAuthDto;
import com.zyb.warehouse.dto.Page;
import com.zyb.warehouse.entity.Auth;
import com.zyb.warehouse.entity.Role;
import com.zyb.warehouse.service.RoleService;
import com.zyb.warehouse.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:角色控制层
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有角色信息
     *
     * @return
     */
    @GetMapping("/role-list")
    public Result queryRoleList() {
        List<Role> roleList = roleService.queryRoleList();
        return Result.ok(roleList);
    }

    /**
     * 条件查询带分页
     *
     * @return
     */
    @GetMapping("/role-page-list")
    public Result pageRole(Page page, Role role) {
        page = roleService.pageQuery(page, role);
        return Result.ok(page);
    }

    /**
     * 添加角色
     *
     * @param role
     * @param token
     * @return
     */
    @PostMapping("/role-add")
    public Result addRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = roleService.addRole(role, token);
        return result;
    }

    /**
     * 启用或禁用角色状态
     *
     * @param role
     * @param token
     * @return
     */
    @PutMapping("/role-state-update")
    public Result modifyState(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = roleService.modifyRoleState(role, token);
        return result;
    }

    /**
     * 修改角色
     *
     * @param role
     * @param token
     * @return
     */
    @PutMapping("/role-update")
    public Result modifyRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = roleService.modifyRole(role, token);
        return result;
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @DeleteMapping("/role-delete/{roleId}")
    public Result deleteRole(@PathVariable("roleId") Integer roleId) {
        Result result = roleService.deleteRole(roleId);
        return result;
    }

    /**
     * 根据角色ID查询已经分配给角色的权限
     *
     * @param roleId
     * @return
     */
    @GetMapping("/role-auth")
    public Result getAllAuthByRoleId(Integer roleId) {
        List<Integer> authList = roleService.queryAllAuthByRoleId(roleId);
        return Result.ok(authList);
    }

    /**
     * 给角色分配权限（添加角色权限）
     *
     * @param assignAuthDto
     * @return
     */
    @PutMapping("/auth-grant")
    public Result assignRoleAuth(@RequestBody AssignAuthDto assignAuthDto) {
        roleService.addRoleAuth(assignAuthDto);
        return Result.ok("权限分配成功");
    }
}
